import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../view/Login/login.vue'
const Welcome = () => import('../component/welcome/welcome.vue')
const Users = () => import('../view/users/users.vue')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    component: Login,
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../view/Home/home.vue'),
    redirect: '/home/welcome',
    children: [
      {
        path: '/home/welcome',
        name: 'welcome',
        component: Welcome,
      },
      {
        path: '/home/users',
        name: 'users',
        component: Users,
      },
      {
        path: '/home/roles',
        name: 'roles',
        component: () => import('../view/roles/roles.vue'),
      },
      {
        path: '/home/goods',
        name: 'goods',
        component: () => import('../view/goods/goods.vue'),
      },
      {
        path: '/home/orders',
        name: 'orders',
        component: () => import('../view/orders/orders.vue'),
      },
      {
        path: '/home/reports',
        name: 'reports',
        component: () => import('../view/reports/reports.vue'),
      },
      {
        path: '/home/rights',
        name: 'rights',
        component: () => import('../view/rights/rights.vue'),
      },
      {
        path: '/home/categories',
        name: 'categories',
        component: () => import('../view/categories/categories.vue'),
      },
      {
        path: '/home/params',
        component: () => import('../view/goods_params/goods_params.vue'),
      },{
        path: '/home/addGoods',
        component: () => import('../view/addGoods.vue'),
      },
    ],
  },
]

const router = new VueRouter({
  mode: 'history',
  routes,
})

router.beforeEach((to, from, next) => {
  const t = window.sessionStorage.getItem('token')
  if (to.path === '/login' || t) {
    next()
  } else {
    next('/login')
  }
})

export default router
